/*
 * Josh Lindoo
 * Login ID : lind6441
 * CS-102, Winter 2013
 * Programming Assignment 4
 * ShowList class: Extension of LinkedList, specifically for storing shows
 * 					
 */

import java.io.*;

public class ShowList extends LinkedList<Showing> {
	
	//default constructor
	ShowList() {
		super();
	}
	
	
	/*
	 * Method: addShow
	 * Purpose: figure out where to add show to the list and then add
	 * Parameters:
	 * 		Showing newShow: show to add
	 * Returns: void
	 */
	
	public void addShow(Showing newShow) {
		
		//empty list
		if(isEmpty()) {
			add(0, newShow);
			return;
		}
		
		Node<Showing> currNode = head; //track curr node
		String newShowTime = newShow.getFullTime(); //new show's time (shorter)
		
		int index = 0; //counter
		//while current node's time is earlier and the next node isn't null
		while(currNode.getDatum().getFullTime().compareTo(newShowTime) < 0 && 
				currNode.getNext() != null) {
			
			index++;
			currNode = currNode.getNext();
				
		}
		
		//if we just hit the end of the list without finding a bigger value
		//adjust index
		if(currNode.getDatum().getFullTime().compareTo(newShowTime) < 0)
			index++;
		add(index, newShow);
		
	}
	
	
	/*
	 * Method: validate
	 * Purpose: validate showlist
	 * Parameters:
	 * 		String dateStr: date to validate with
	 * Returns: void
	 */
	
	public void validate(String dateStr) {
		
		//empty list
		if(isEmpty()) return;
		
		Node<Showing> currNode = head; //track curr node
		
		final int INDEX = 0; //remove at index 0
		//while selected entry's time comes before validate date
		while(currNode.getDatum().getFullTime().compareTo(dateStr) < 0 && 
				currNode.getNext() != null) {
			
			currNode = currNode.getNext();
			remove(INDEX);
				
		}
		
		//account for reaching the end without finding later time
		if(currNode.getDatum().getFullTime().compareTo(dateStr) < 0)
			remove(INDEX);
		
	}
	
	/*
	 * Method: printList
	 * Purpose: print list contents
	 * Parameters:	none
	 * Returns: void
	 */
	
	public void writeToDisk(BufferedWriter showFile) {
		
		Node<Showing> currNode = head; //track curr node
		
		while(currNode != null) {
			
			try {
				showFile.append(currNode.getDatum().export());
				showFile.newLine();
			}
			catch(IOException exc) {
				System.out.println("Unable to print to show file..");
			}
			
			currNode = currNode.getNext();
		}
		
	}

}
